package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.MyCollectNote;
import com.lsy.weizhi2.JsonObject.WoYueJson;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.NoteDetail;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.customview.GlideCircleTransform;
import com.lsy.weizhi2.myutils.IAskAdapterCall;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 嗨哟 on 2017/5/5.
 */

public class iAskAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<WoYueJson.ListBean> list;
    private LayoutInflater layoutInflater;
    private int flag;
    private int bottom_count,tag;//是否显示底部，底部显示文字
    private IAskAdapterCall iAskAdapterCall;

    public iAskAdapter(Context context, List<WoYueJson.ListBean> list,int flag) {
        this.context = context;
        this.list = list;
        this.flag=flag;
        layoutInflater=LayoutInflater.from(context);
    }

    public void UpData(List<WoYueJson.ListBean> list,int bottom_count,int tag){
        this.list = list;
        this.bottom_count=bottom_count;
        this.tag=tag;
        this.notifyDataSetChanged();
    }

    public void UpData_Position(List<WoYueJson.ListBean> list,int bottom_count,int tag,int position){
        this.list = list;
        this.bottom_count=bottom_count;
        this.tag=tag;
        this.notifyItemChanged(position);
    }

    public void setiAskAdapterCall(IAskAdapterCall iAskAdapterCall){
        this.iAskAdapterCall=iAskAdapterCall;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view_yue=layoutInflater.inflate(R.layout.manager_item_yue,viewGroup,false);
        View view_bottom = layoutInflater.inflate(R.layout.bottom_view,viewGroup,false);
        AutoUtils.autoSize(view_yue);
        AutoUtils.autoSize(view_bottom);
        if(i== ManagerItem.Bottom){
            return new BottomHolder(view_bottom);
        }else{
            return new YueDanViewHolder(view_yue);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof YueDanViewHolder){
            ((YueDanViewHolder) holder).position=position;
            if(flag==0){
                ((YueDanViewHolder) holder).yue_dan_cancel.setText("取消预约");
                ((YueDanViewHolder) holder).yue_dan_state.setText("预约中");
            }else{
                ((YueDanViewHolder) holder).yue_dan_cancel.setText("拒绝");
                ((YueDanViewHolder) holder).yue_dan_state.setText("接单");
            }
            if(list!=null&&list.size()!=0){
                ((YueDanViewHolder) holder).yue_dan_name.setText(list.get(position).getServiceProvider().getRealName());
                ((YueDanViewHolder) holder).yue_dan_main.setText(list.get(position).getOrder().getTitle());
                ((YueDanViewHolder) holder).yue_dan_words.setText(list.get(position).getOrder().getContent());
                ((YueDanViewHolder) holder).yue_dan_time.setText(list.get(position).getCreateTime());
                int status=list.get(position).getStatus();
                if(status==0&&flag==0){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("预约中");
                }else if(status==-5){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已拒绝");
                }else if(status==20){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已接单");
                }else if(status==30){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已完成");
                }else if(status==40){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已评价");
                }else if(status==-10){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已取消");
                }
                Glide.with(context).load(new GlideUrl(Constants.DOWN_USER_PICTURE+list.get(position).getServiceProvider().getAvatar(),new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID",MyInfo.User_Id+"").build()))
                        .transform(new GlideCircleTransform(context))
                        .error(R.mipmap.default_head).into(((YueDanViewHolder) holder).yue_dan_head_image);
            }
        }else if(holder instanceof BottomHolder){
            if(bottom_count==1&&tag==0){
                ((BottomHolder) holder).bottom_tv.setText("松开加载");
            }else if(bottom_count==1&&tag==1){
                ((BottomHolder) holder).bottom_tv.setText("正在加载");
            }else if(bottom_count==1&&tag==2){
                ((BottomHolder) holder).bottom_tv.setText("到底了");
            }
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(list!=null&&position==list.size()){
            return ManagerItem.Bottom;
        }else{
            return super.getItemViewType(position);
        }

    }

    @Override
    public int getItemCount() {
        return list==null?0+bottom_count:list.size()+bottom_count;
    }

    class YueDanViewHolder extends RecyclerView.ViewHolder{
        int position;
        RelativeLayout yue_item_view;
        ImageView yue_dan_head_image;
        TextView yue_dan_name,yue_dan_time,yue_dan_renzheng,
                yue_dan_main,yue_dan_words,yue_dan_state,yue_dan_cancel;
        public YueDanViewHolder(View itemView) {
            super(itemView);
            yue_item_view= (RelativeLayout) itemView.findViewById(R.id.yue_item_view);
            yue_dan_head_image= (ImageView) itemView.findViewById(R.id.yue_dan_head_image);
            yue_dan_name= (TextView) itemView.findViewById(R.id.yue_dan_name);
            yue_dan_time= (TextView) itemView.findViewById(R.id.yue_dan_time);
            yue_dan_renzheng= (TextView) itemView.findViewById(R.id.yue_dan_renzheng);
            yue_dan_main= (TextView) itemView.findViewById(R.id.yue_dan_main);
            yue_dan_words= (TextView) itemView.findViewById(R.id.yue_dan_words);
            yue_dan_state= (TextView) itemView.findViewById(R.id.yue_dan_state);
            yue_dan_cancel= (TextView) itemView.findViewById(R.id.yue_dan_cancel);
            yue_dan_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch(yue_dan_cancel.getText().toString()){
                        case "取消预约":
                            iAskAdapterCall.Cancel(position);
                            break;
                        case "拒绝":
                            iAskAdapterCall.Refuse(position);
                            break;
                    }

                }
            });
            yue_dan_state.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (yue_dan_state.getText().toString()){
                        case "接单":
                            iAskAdapterCall.Accept(position);
                            break;

                    }
                }
            });
            yue_item_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Gson gson=new Gson();
                    if(list!=null&&list.get(position)!=null){
                        String json=gson.toJson(list.get(position));
                        MyMethod.toActivity(context,NoteDetail.class,new String[]{"json","tag"},new String[]{json,ManagerItem.CollectWeiZhi+""});
                    }
                }
            });
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder{
        TextView bottom_tv;
        public BottomHolder(View itemView) {
            super(itemView);
            bottom_tv= (TextView) itemView.findViewById(R.id.bottom_tv);
        }
    }

}
